package com.moon.common;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;

import com.moon.constants.MoonConstants;
import com.moon.model.User;

/**
 * @author paul
 * @date 2014年5月2日
 */
public class AbstractActivity extends Activity {
    protected ManagedGoogleTracker tracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tracker = ManagedGoogleTracker.getInstanceAndIncreaseCount(getApplication());
    }

    @Override
    protected void onDestroy() {
        tracker.decreaseActivityCount();
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        tracker.trackPageView("/" + getClass().getSimpleName());
        super.onResume();
    }

    protected User getUser() {
        User user = new User();
        SharedPreferences shareDate = getSharedPreferences(MoonConstants.SHARED_PREFERENCES_KEY,
                Context.MODE_PRIVATE);
        if (shareDate == null) {
            return user;
        }
        user.setEmail(shareDate.getString(MoonConstants.USER_EMAIL, null));
        if(shareDate.getString(MoonConstants.USER_USER_ID, null) != null){
            user.setId(Long.valueOf(shareDate.getString(MoonConstants.USER_USER_ID, null)));
        }
        user.setImgUrl(shareDate.getString(MoonConstants.USER_HEAD_IMGURL, null));
        user.setIntroduce(shareDate.getString(MoonConstants.USER_INTRODUCE, null));
        user.setLoginId(shareDate.getString(MoonConstants.USER_LOGIN_ID, null));
        user.setName(shareDate.getString(MoonConstants.USER_NAME, null));
        return user;
    }

}
